package com.fau.wm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalWarehouse;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 仓库管理Action
 * @author liy
 * @datetime 2014年7月26日,下午6:03:10
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class WarehouseAction extends PublicAction implements ModelDriven<MedicalWarehouse> {

	private static final long serialVersionUID = -7829175277286588654L;
	
	private MedicalWarehouse warehouse = new MedicalWarehouse();
	
	/**
	 * @desc 根据医院id查询仓库
	 * @return
	 * @author liy
	 * @datetime 2014年7月28日,下午2:05:06
	 */
	@Action(value = "warehouse_hopital_list")
	public String warehouseFindByHopital() {
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.list = this.baseAllService.getBaseDataService().findWarehouseByHospital(admin.getEnterpriseId());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.list);
		return null;
	}
	
	/**
	 * @desc 删除供应商信息
	 * @return null
	 * @author liy
	 * @datetime 2014年7月27日,上午11:54:23
	 */
	@Action(value = "warehouse_delete")
	public String delete() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(warehouse.getId())){
			boolean flag = this.baseAllService.getBaseDataService().deleteWarehouse(this.warehouse);
			if(flag){
				this.map.put("success", true);
			}else{
				this.map.put("success", false);
			}
			formatJSON(response, this.map);
		}
		return null;
	}
	
	/**
	 * @desc 保存仓库信息
	 * @return null
	 * @author liy
	 * @datetime 2014年7月27日,上午9:58:13
	 */
	@Action(value = "warehouse_save")
	public String save() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = new HashMap<String, Object>();
		Boolean flag =  this.baseAllService.getBaseDataService().saveWarehouse(this.warehouse, admin);
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 修改并保存仓库信息
	 * @return null
	 * @author liy
	 * @datetime 2014年8月1日,下午8:01:52
	 */
	@Action(value = "warehouse_edit_save")
	public String edit() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(StringUtils.isBlank(this.warehouse.getId())) {
			this.map.put("success", false);
			this.map.put("message", "保存时发生错误，ID为空！");
		}else {
			this.baseAllService.getBaseDataService().editWarehouse(this.warehouse);
			this.map.put("success", true);
		}
		
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 跳转至仓库信息添加页面
	 * @return SUCCESS
	 * @author liy
	 * @datetime 2014年7月26日,下午6:54:41
	 */
	@Action(value = "warehouse_add", results = {@Result(location = "warehouse_add.jsp")})
	public String warehouseAdd() {
		return SUCCESS;
	}
	
	/**
	 * @desc 跳转至仓库信息修改页面
	 * @return SUCCESS
	 * @author liy
	 * @datetime 2014年8月1日,下午7:49:44
	 */
	@Action(value = "warehouse_edit", results = {@Result(location = "warehouse_edit.jsp")})
	public String warehouseManager() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询供应商信息
	 * @return null
	 * @author liy
	 * @datetime 2014年7月26日,下午6:38:08
	 */
	@Action(value = "warehouse_page")
	public String warehousePage() {
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		warehouse.setHospitalId(admin.getEnterpriseId());
		this.pageBean = this.baseAllService.getBaseDataService().findWarehouseByPage(this.warehouse, this.map);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 跳转至仓库管理页面
	 * @return SUCCESS
	 * @author liy
	 * @datetime 2014年7月26日,下午6:18:01
	 */
	@Action(value = "warehouse_init", results = {@Result(location = "warehouse_init.jsp")})
	public String warehouseList() {
		return SUCCESS;
	}

	@Override
	public MedicalWarehouse getModel() {
		return warehouse;
	}

}
